import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

import InputElement from "../components/InputElement";
import { reg, usernameExist } from "../services/user";
import { Button, Card, Form, Input,Modal } from "antd";

function Reg(props) {
    const navigate = useNavigate();
    const onFinish = async (values) => {
        await reg(values.username,values.pwd);
        Modal.success({
          content:"注册成功",
          onOk(){
            navigate("/login");
          }
        });
        // console.log("Success:", values);
    };
    const onFinishFailed = (errorInfo) => {
        console.log("Failed:", errorInfo);
    };

    return (
        <Card
            title="注册"
            style={{
                width: 600,
                margin: "auto",
            }}
        >
            <Form
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    hasFeedback
                    rules={[
                        {
                            required: true,
                            message: "请输入用户名",
                        },
                        {
                          pattern:/^\w{6,20}$/,
                          message: "用户名必须位6-20位字母、数字、_构成",
                        },
                        {
                          async validator(_,value){
                            console.log("value",value)
                            let data = await usernameExist(value);
                            if(data.status == 1){
                              return Promise.resolve();
                            }else{
                              return Promise.reject(new Error("用户名已存在"));
                            }
                            
                          }
                        }
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="pwd"
                    rules={[
                        {
                            required: true,
                            message: "请输入密码",
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    label="确认密码"
                    name="confirm"
                    rules={[
                        {
                            required: true,
                            message: "请输入确认密码",
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        Submit
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    );
}
export default Reg;
